home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / DELPHI32 / GRAPHICS / TS32 / EXPLOSIO.PAS < prev    next >
Pascal/Delphi Source File  |  1996-03-14  |  2KB  |  96 lines

  1. unit Explosion;
  2.  
  3. (*********************************************
  4. TExplosion->TSprite
  5.  
  6. A re-usable sprite class that generates an
  7. explosion effect through particles.
  8. *********************************************)
  9.  
  10. interface
  11.  
  12. uses
  13.   Windows, SysUtils, Classes, Graphics, Controls, DIBDrawingSurface, Sprite,
  14.   Grafix, Utility;
  15.  
  16. type
  17.  
  18.   TExplBits = record
  19.      pt: TPoint;
  20.      x, y: integer;
  21.   end;
  22.  
  23.   TExplosion = class( TSprite )
  24.   private
  25.      FLife: integer;
  26.      FCol: byte;
  27.      arParticles: array[1..25] of TExplBits;
  28.   protected
  29.   public
  30.      constructor CreateExplosion( pt: TPoint; nLife: integer; nColor: byte );
  31.      procedure Move; override;
  32.      procedure Render; override;
  33.   end;
  34.  
  35. implementation
  36.  
  37. constructor TExplosion.CreateExplosion( pt: TPoint; nLife: integer; nColor: byte );
  38. var
  39.   i: integer;
  40. begin
  41.   inherited Create;
  42.   FCol := nColor;
  43.   FLife := nLife;
  44.   ptPosition := pt;
  45.   for i := 1 to 25 do
  46.      with arParticles[i] do
  47.         begin
  48.            pt.X := ptPosition.X;
  49.            pt.Y := ptPosition.Y;
  50.            x := Random( 200 );
  51.            if Random( 100 ) > 50 then
  52.               x := -x;
  53.            y := Random( 200 );
  54.            if Random( 100 ) > 50 then
  55.               y := -y;
  56.         end;
  57. end;
  58.  
  59. procedure TExplosion.Move;
  60. begin
  61.   inherited Move;
  62.   if FLife > 0 then
  63.      begin
  64.         Dec( FLife );
  65.         if FLife = 0 then
  66.            Dead := TRUE;
  67.      end;
  68. end;
  69.  
  70. procedure TExplosion.Render;
  71. var
  72.   i: integer;
  73.   x_, y_: integer;
  74. begin
  75.   inherited Render;
  76.   for i := 1 to 25 do
  77.      with arParticles[i] do
  78.         begin
  79.            if Random( 200 ) > Abs( x ) then
  80.               if x < 0 then
  81.                  Dec( pt.X )
  82.               else
  83.                  Inc( pt.X );
  84.            if Random( 200 ) > Abs( y ) then
  85.               if y < 0 then
  86.                  Dec( pt.Y )
  87.               else
  88.                  Inc( pt.Y );
  89.            if ( pt.X >= 0 ) and ( pt.X < dds.PhysicalWidth ) and
  90.               ( pt.Y >= 0 ) and ( pt.Y < dds.PhysicalHeight ) then
  91.               dds.DIBCanvas.Pixels[pt.X, pt.Y] := Random( 16 ) + FCol;
  92.         end;
  93. end;
  94.  
  95. end.
  96.